package lumis.content.control.button;

import java.util.Collection;


import lumis.doui.control.IControl;
import lumis.doui.control.button.ButtonControl;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.util.XmlUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Responsible for opening the preview interface for the selected content 
 * if the service has some preview supported interface.
 *
 * Example use:
 * <xmp>
 * <control:lum_previewButton id="buttonPreview"/>
 * </xmp>
 * 
 * xml data available for xsl rendering:
 * <xmp>
 *	<control id="previewButton" text="STR_PREVIEW" type="lum_previewButton">
 *		<onClick type="popupRunTimeInterface">
 *			<interfaceId>lumis.service.content.preview.previewContent</interfaceId> 
 *			<pageId>LumisBlankPage</pageId> 
 *			<validators>
 *				<validator controlId="adminList.tabulardata" type="selectedOne" /> 
 *			</validators>
 *			<addParameters>
 *				<parameter name="itemId">
 *					<value controlId="adminList.tabulardata" type="firstSelected" /> 
 *				</parameter>
 *				<parameter name="callerServiceInstanceId">
 *					<value>8A488A0F14A741560114A74CDE8501E0</value> 
 *				</parameter>
 *				<parameter name="callerSourceId">
 *					<value>default</value> 
 *				</parameter>
 *			</addParameters>
 *		</onClick>
 *		<image>lumis/portal/client/images/PreviewContent.gif</image> 
 *		<data>
 *			<name>Visualizar</name> 
 *			<onclick><!-- javascript for onClick event --></onclick> 
 *		</data>
 *	</control>
 * </xmp>
 * 
 * @version $Revision: 9025 $ $Date: 2008-03-13 16:34:51 -0300 (Thu, 13 Mar 2008) $
 * @since 4.1.0
 */
public class PreviewButtonControl extends ButtonControl
{
	private static ILogger logger = LoggerFactory.getLogger(PreviewButtonControl.class);
	
	public void setRenderData() throws PortalException
	{
		// detect if preview button should be available
		String serviceId = douiContext.getRequest().getServiceConfig().getId();
		
		boolean isPreviewSupported = false;
		try
		{
			Collection<String> ServiceInterfaceIds = ManagerFactory.getServiceInterfaceManager().getIdsByServiceId(serviceId, douiContext.getTransaction());
			for(String serviceInterfaceId: ServiceInterfaceIds)
			{
				ServiceInterfaceConfig serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(serviceInterfaceId,  douiContext.getTransaction());
				if(serviceInterfaceConfig.isPreviewSupported())
				{
					isPreviewSupported = true;
					break;
				}
			}
		}
		catch (UnexpectedException e) 
		{
			throw e;
		}
		
		// if no interface Id is specified, remove the add button
		if(!isPreviewSupported)
		{
			logger.debug("No preview supported interface found. Removing the control.");

        	parentControl.removeChild(this);
			return;
		}
		
		if(XmlUtil.selectSingleNode("onClick", controlDefinitionNode) == null)
		{
			String interfaceId = XmlUtil.readAttributeString("interfaceId", controlDefinitionNode, "lumis.service.content.preview.previewContent");
			String listControlId = XmlUtil.readAttributeString("listControlId", controlDefinitionNode);
			String serviceInstanceId = douiContext.getRequest().getServiceInstanceConfig().getId();
			String sourceId = getSource().getId();
			
			if(listControlId == null)
			{
				IControl listControl = controlContainer.getControlByType("lum_advancedTabularData");
				if(listControl != null)
					listControlId = listControl.getId();
			}
			
			StringBuilder onClickXml = new StringBuilder();
			onClickXml.append(XmlUtil.XML_HEADER+"<onClick type=\"popupRunTimeInterface\">");
			onClickXml.append("<interfaceId>");
			onClickXml.append(interfaceId);
			onClickXml.append("</interfaceId>");
			onClickXml.append("<pageId>LumisBlankPage</pageId>");		
			onClickXml.append("<validators>");
			onClickXml.append("<validator type=\"selectedOne\" controlId=\"");
			onClickXml.append(listControlId);
			onClickXml.append("\" />");
			onClickXml.append("</validators>");
			onClickXml.append("<addParameters>");
			onClickXml.append("<parameter name=\"itemId\">");
			onClickXml.append("<value type=\"firstSelected\" controlId=\"");
			onClickXml.append(listControlId);
			onClickXml.append("\" />");
			onClickXml.append("</parameter>");
			onClickXml.append("<parameter name=\"callerServiceInstanceId\">");
			onClickXml.append("<value>");
			onClickXml.append(serviceInstanceId);
			onClickXml.append("</value>");
			onClickXml.append("</parameter>");
			onClickXml.append("<parameter name=\"callerSourceId\">");
			onClickXml.append("<value>");
			onClickXml.append(sourceId);
			onClickXml.append("</value>");
			onClickXml.append("</parameter>");
			onClickXml.append("</addParameters>");		
			onClickXml.append("</onClick>");
			
			XmlUtil.addNodeFromXmlString(controlDefinitionNode, onClickXml.toString());
		}

		if(XmlUtil.selectSingleNode("image", controlDefinitionNode) == null)
		{
			XmlUtil.addTextNode(controlDefinitionNode, "image", "lumis/portal/client/images/PreviewContent.gif");
		}

		if(XmlUtil.readAttributeString("text", controlDefinitionNode) == null)
		{
			XmlUtil.addNodeAttr(controlDefinitionNode, "text", "STR_PREVIEW");
		}
		
		super.setRenderData();
	}
}
